import Taro, { getStorageInfoSync as _getStorageInfoSync, getStorageSync as _getStorageSync, setStorageSync as _setStorageSync, removeStorage as _removeStorage } from "@tarojs/taro-h5";
import Utils from './utils';
const __DEBUG = true;
var __DATA = new Map();
var initialized = false;
const Global = {
  isDebug: true,
  config: {
    apiServerDebug: 'http://localhost:10085/api/v1',
    apiServer: 'https://ms2.hybaodigital.com/api/v1',
    cdnServer: 'https://cdn.hybaodigital.com/ms2'
  },
  init: () => {
    // 检查local里是否有缓存
    try {
      const res = _getStorageInfoSync();
      Utils.debug('res.keys=', res.keys);
      res.keys.map(key => {
        let value = _getStorageSync(key);
        __DATA.set(key, value);
      });
      initialized = true;
    } catch (e) {
      // Do something when catch error
    }
  },
  set: (key, val, type = 0) => {
    let value = {
      type: type,
      data: val
    };
    __DATA.set(key, value);
    if (type == 1) {
      // 存本地
      _setStorageSync(key, value);
    }
    Utils.debug('global.set(\'' + key + '\')=', val);
  },
  get: key => {
    if (!initialized) {
      Global.init();
    }
    let value = __DATA.get(key);
    if (value) {
      return value.data;
    } else {
      return value;
    }
  },
  has: key => {
    if (!initialized) {
      Global.init();
    }
    return __DATA.has(key);
  },
  remove: key => {
    __DATA.delete(key);
    // 移除本地缓存
    _removeStorage({ key: key });
  }
};
export default Global;